package br.com.sistema.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import br.com.sistema.conexao.Entity;
import br.com.sistema.models.Endereco;
import br.com.sistema.models.Entidade;


public class EnderecoDao implements IDao<Endereco> {

	@Override
	public Endereco getById(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Endereco> getByCriteria(Endereco o) throws Exception {
		try {
			
			EntityManager em = Entity.createEm();
			List<Endereco> list = null;
			
			try {
				
				CriteriaBuilder cb = em.getCriteriaBuilder();
				CriteriaQuery c = cb.createQuery(Endereco.class);
				Root<Endereco> a = c.from(Endereco.class);
				c.select(a);
				
				List<Predicate> predicates = new ArrayList<Predicate>();
				
				if(o.getEntidade() != null){
					predicates.add(cb.equal(a.<Entidade>get("entidade"), o.getEntidade()));
				}
				
				if(o.getEndecep() != ""){
					predicates.add(cb.like(a.<String>get("endecep"), "%"+o.getEndecep()+"%"));
				}
				
				if(o.getEndelogradouro() != ""){
					predicates.add(cb.like(a.<String>get("endelogradouro"), "%"+o.getEndelogradouro()+"%"));
				}
				
				c.where(cb.and(predicates.toArray(new Predicate[]{})));
				
				TypedQuery<Endereco> query = em.createQuery(c);
				list = query.getResultList();
				
				return list;

			} catch (Exception e) {
				throw e;
			}finally{
				em.close();
			}
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void remove(List<Endereco> list) throws Exception {
		
		try {
			EntityManager em = Entity.createEm();
			try {
				em.getTransaction().begin();
				for(Endereco p : list){
					Endereco i = em.find(Endereco.class, p.getEndeid());
					em.remove(i);
				}
				em.getTransaction().commit();
			} catch (Exception e) {
				if(em.getTransaction().isActive()){
					em.getTransaction().rollback();
				}
				throw e;
			}finally{
				em.close();
			}
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public List<Endereco> getAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Endereco manter(Endereco o) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Endereco insere(Endereco o) throws Exception {
		try {
			EntityManager em = Entity.createEm();
			try {
				em.getTransaction().begin();
				em.persist(o);
				em.getTransaction().commit();
				return o;
			} catch (Exception e) {
				if(em.getTransaction().isActive()){
					em.getTransaction().rollback();
				}
				throw e;
			}finally{
				em.close();
			}
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Endereco altera(Endereco o) throws Exception {
		
		try {
			
			EntityManager em = Entity.createEm();
			Endereco endereco = em.find(Endereco.class, o.getEndeid());
			
			try {
				
				em.getTransaction().begin();
				endereco.setTipoendereco(o.getTipoendereco());
				endereco.setMunicipio(o.getMunicipio());
				endereco.setEntidade(o.getEntidade());
				endereco.setEndecep(o.getEndecep());
				endereco.setEndelogradouro(o.getEndelogradouro());
				endereco.setEndenumero(o.getEndenumero());
				endereco.setEndebairro(o.getEndebairro());
				endereco.setEndecomplemento(o.getEndecomplemento());
				endereco.setXusuario(o.getXusuario());
				em.persist(endereco);
				em.getTransaction().commit();
				
				return endereco;
				
			} catch (Exception e) {
				
				if(em.getTransaction().isActive()){
					em.getTransaction().rollback();
				}
				
				throw e;
				
			}finally{
				em.close();
			}
		} catch (Exception e) {
			throw e;
		}
	}

	

}
